<?php
/**
 * Created by 柚子i.
 * User: Emperor
 * Date: 2019/7/18
 * Time: 17:02
 */

namespace app\common\service\control;

use app\common\model\control\ControlPlanName;
use app\common\service\BaseService;
use think\Db;

class ControlPlanTrack extends BaseService
{
    /**
     * Notes :获取无效预案列表
     * @param $param
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \Exception
     * @author lichao   2019/8/15
     */
    public function getInvalidPlan($param)
    {
        try {
            $planName = isset($param['plan_name']) ? $param['plan_name'] : '';
            $page     = isset($param['page']) ? $param['page'] : 1;
            $where    = [
                ["step", "<", "5"],
                ["track", "=", 0],
                ["control_start_time", "<>", date("Y-m-d 00:00:00", strtotime("+1 day"))],
            ];

            $planList = $this->getPlanResult($planName, $where);

            return $planList;
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Notes :获取完成预案列表
     * @param $param
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \Exception
     * @author lichao   2019/7/19
     */
    public function getFinishPlan($param)
    {
        try {
            $planName = isset($param['plan_name']) ? $param['plan_name'] : '';
            $page     = isset($param['page']) ? $param['page'] : 1;
            $where    = [
                "step"  => 6,
                "track" => 1,
            ];

            $planList = $this->getPlanResult($planName, $where);

            return $planList;
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Notes :获取待评估预案列表
     * @param $param
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \Exception
     * @author lichao   2019/7/19
     */
    public function getAssessPlan($param)
    {
        try {
            $planName = isset($param['plan_name']) ? $param['plan_name'] : '';
            $page     = isset($param['page']) ? $param['page'] : 1;
            $where    = [
                "step"  => 6,
                "track" => 0,
            ];

            $planList = $this->getPlanResult($planName, $where);

            return $planList;
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Notes :获取跟踪预案列表
     * @param $param
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \Exception
     * @author lichao   2019/7/19
     */
    public function getTrackPlan($param)
    {
        try {
            $planName = isset($param['plan_name']) ? $param['plan_name'] : '';
            $page     = isset($param['page']) ? $param['page'] : 1;
            $where    = [
                "step"  => 5,
                "track" => 0,
            ];

            $planList = $this->getPlanResult($planName, $where);

            return $planList;
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Notes :根据条件查找对应预案列表
     * @param $planName
     * @param $where
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author lichao   2019/8/15
     */
    public function getPlanResult($planName, $where)
    {
        $planList = Db::name('control_plan_name')
            ->alias('name')
            ->field(["name.id, name.plan_name, name.plan_number, name.plan_type, name.control_start_time,
                 name.control_end_time, name.response_start_time, name.response_end_time, track.step, track.track"])
            ->join('control_plan_state_track track', 'name.plan_number = track.plan_number')
            ->where($where)
            ->where('name.plan_name|name.plan_number', 'like', "%" . $planName . "%")
            ->order('name.id', 'desc')
            // ->limit(10)->page($page)
            ->select();

        return $planList;
    }

    /**
     * Notes :获取预案状态跟踪
     * @param $planType
     * @param $planNumber
     * @return mixed
     * @author lichao   2019/7/18
     */
    public function getControlTrackState($planType, $planNumber)
    {
        $controlPlanTrackSql  = "SELECT `step`, `track` FROM control_plan_state_track WHERE `plan_type` = ? AND
 `plan_number` = ?";
        $controlPlanTrackData = Db::query($controlPlanTrackSql, array(
            $planType, $planNumber));

        return $controlPlanTrackData[0];
    }

    /**
     * Notes :修改预案状态跟踪
     * @param $step
     * @param $track
     * @param $planNumber
     * @return int
     * @author lichao   2019/7/18
     */
    public function updateControlTrackState($step, $track, $planNumber)
    {
        $controlPlanTrackSql = "UPDATE control_plan_state_track SET `step` = ?, `track` = ? WHERE `plan_number` = ?";

        $controlPlanTrackStatus = Db::execute($controlPlanTrackSql, array(
            $step, $track, $planNumber));

        return $controlPlanTrackStatus;
    }
}
